<template lang="html">
  <div class="keywork">
      <el-form :inline="true" class="demo-form-inline" ref='searchForm'>
        <el-form-item label="关键字">
          <input class='el-input__inner' type='text' @keydown.enter='search' v-model='searchForm.key' placeholder="请输入关键字">
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click='search'>查询</el-button>
        </el-form-item>
      </el-form>

      <el-button type="primary" @click='add'>添加</el-button>

      <el-table
        :data="tableData"
        style="width:1000px">
        <el-table-column
          type="index"
          width="50">
        </el-table-column>
        <el-table-column
          prop="key"
          label="关键词">
        </el-table-column>
        <el-table-column
          prop="date"
          label="时间">
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click='edit(scope)'>编辑</el-button>
            <el-button type="danger" @click='del(scope)'>删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pageBlock">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          layout="prev, pager, next, jumper, sizes"
          :total="totalData">
        </el-pagination>
      </div>
  </div>
</template>
<script>
import data from './data/keyWordData';
export default {

  data () {
    return {
      tableData:[],
      pickerValue:[],
      searchForm:{
        key:''
      },
      loading:false,
      pageSize:50,
      currentPage:1
    }
  },
  computed:{
    totalData(){
      return this.tableData.length
    }
  },
  methods:{
    updateData(){
      tools.setLocal('keyWordListV2',this.tableData);
    },
    search(){
      let search = tools.cloneObj(this.searchForm);
      for(let i in search){
        if(search[i] === ''){
          delete search[i]
        }
      }

      if(!tools.emptyObj(search)){
        let arr = [];
        for(let i of data){
          let has = true;
          for(let o in search){
            if(i[o].indexOf(search[o]) == -1){
              has = false;
            }
          }

          if(has){
            arr.push(i);
          }
        }

        this.tableData = arr;
      }else{
        this.tableData = data;
      }
    },
    add(){
      this.$prompt('请输入关键词', '添加', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValue:'',
        inputPattern: /^.{1,10}$/,
        inputErrorMessage: '关键词控制在1~10个字符'
      })
      .then(({ value }) => {

        this.tableData.unshift({
          date:new Date().Format('yyyy-MM-dd hh:mm:ss'),
          key:value
        });

        this.$nextTick(function(){
          this.updateData();
        });

        tools.toastOk('添加成功');
      })
      .catch(e => {
      })
    },
    edit(scope){
      this.$prompt('请输入关键词', '修改', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValue:scope.row.key,
        inputPattern: /^.{1,10}$/,
        inputErrorMessage: '关键词控制在1~10个字符'
      })
      .then(({ value }) => {

        this.$set(this.tableData[scope.$index],'date',new Date().Format('yyyy-MM-dd h:m:s'));
        this.$set(this.tableData[scope.$index],'key',value);

        this.$nextTick(function(){
          this.updateData();
        });

        tools.toastOk('修改成功');
      })
      .catch(e => {
      })

    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      // this.getList();
    },
    del(scope){
      this.$confirm('是否确认删除？',{center:true})
      .then(res =>{

        this.loading = true;

        this.tableData.splice(scope.$index,1);

        tools.toastOk('删除成功');
        
        this.$nextTick(function(){
          this.updateData();
          this.loading = false;
        });

      }).catch( e=> {})
    },
    tourl(url,query){
      tools.router.push({path:url,query:query || {}})
    }
  },
  mounted(){
    let list = tools.getLocal('keyWordListV2');

    if(list){
      this.tableData = list;
    }else{
      this.tableData = data;
      tools.setLocal('keyWordListV2',data);
    }
  },
  components:{

  }
}
</script>

<style scoped lang='scss'>
@import '../../assets/scss/tool.scss';
.keywork{

}
</style>
